import queryString from 'query-string';
import { param2Obj } from '@/utils/index';
const { APIURl } = require('@/utils/apiUrl');
const flowJumpUrl = APIURl.replace('/flow', '');

export function getIframeUrl(formData = {}, urlAddress, isDone = '') {
  let pageUrl = '';
  if (urlAddress) {
    // urlAddress = `/hr-ui/#/salaryManage/reportsManage/detailOA?id=id&app=app&type=businessType&companyId=id`; // 调试用
    // url上的参数名与formData中的key不一致时, 在配置Web地址时可以设置为 xxxx/xxx?type=businessType&id=companyId
    const baseUrl = urlAddress.split('?')[0];
    const query = param2Obj(urlAddress);
    for (const key in query) {
      const value = query[key] ? formData[query[key]] : formData[key];
      query[key] = value || query[key];
    }
    isDone && (query.isDone = isDone);
    const paramsUrl = queryString.stringify(query);
    pageUrl = baseUrl + '?' + paramsUrl;
  }
  return pageUrl ? (pageUrl.includes('http') ? pageUrl : flowJumpUrl + pageUrl) : '';
}
